package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IBedTableDao;
import com.trkj.dao.jpa.ICardDao;
import com.trkj.dao.jpa.IDepositTableDao;
import com.trkj.dao.jpa.IHospWorkInNoticeDao;
import com.trkj.dao.jpa.ILiveinrecordTableDao;
import com.trkj.dao.jpa.IStaffDao;
import com.trkj.dao.jpa.IVisitTableDao;
import com.trkj.dao.mybatis.LiveinrecordTableMapper;
import com.trkj.dao.mybatis.VisitTableMapper;
import com.trkj.pojos.BedTable;
import com.trkj.pojos.Card;
import com.trkj.pojos.DepositTable;
import com.trkj.pojos.HospWorkInNotice;
import com.trkj.pojos.LiveinrecordTable;
import com.trkj.pojos.Staff;
import com.trkj.pojos.VisitTable;
@Service
@Transactional
public class LiveInService {
	@Autowired
	private ILiveinrecordTableDao iLiveinrecordDao;
	@Autowired
	private IVisitTableDao iVisitTableDao;
	@Autowired
	private LiveinrecordTableMapper liveinrecordTableMapper;
	@Autowired
	private VisitTableMapper visitTableMapper;
	@Autowired
	private IBedTableDao iBedTableDao;
	@Autowired
	private ICardDao iCardDao;
	@Autowired
	private IStaffDao iStaffDao;
	@Autowired
	private DepositService depositService;
	@Autowired
	private IDepositTableDao iDepositTableDao;
	@Autowired
	private IHospWorkInNoticeDao iHospWorkInNoticeDao;
	/**
	 * 新增住院
	 * @param lt
	 * @param despoit
	 * @param DoctorID
	 * @param MedicalId
	 * @param BedId
	 * @return
	 */
	public boolean addLiveInPatient(LiveinrecordTable lt,BigDecimal despoit,
			String DoctorID,String MedicalId,Long BedId){
		
		try {
			BedTable bed = iBedTableDao.findOne(BedId);
			Card card = iCardDao.findOne(MedicalId);
			Staff doctor = iStaffDao.findOne(DoctorID);
			lt.setLivestatus("在院");//将住院设置为默认在院
			lt.setBedTable(bed);//与病床表建立关系
			lt.setCard(card);//与诊疗卡建立关系
			lt.setStaff(doctor);//与医生建立关系
			iLiveinrecordDao.save(lt);//保存
			
			bed.setBedstatus("已住");//修改床位状态
			iBedTableDao.save(bed);//保存
			
			HospWorkInNotice notic = iHospWorkInNoticeDao.findOne(Long.parseLong(lt.getLivenoticid3()));
			notic.setLivestatus("已处理");//修改通知状态
			iHospWorkInNoticeDao.save(notic);
			
			//新建住院押金
			String DepositID = depositService.createDepositID();
			DepositTable depositTable = new DepositTable();
			depositTable.setDepositid2(DepositID);
			depositTable.setDepositlimit4(despoit);
			depositTable.setDepositstatus("未缴");
			depositTable.setLiveid5(lt.getLiveid8());
			depositTable.setLivenoticid3(lt.getLivenoticid3());
			depositTable.setLiveoffice4(lt.getLiveoffice4());
			depositTable.setPatientname5(lt.getPatientname8());
			depositTable.setTaketime(lt.getLivedate());
			depositTable.setTakelimit(new BigDecimal(0));
			iDepositTableDao.save(depositTable);//保存
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 动态分页查询住院信息
	 * @param map
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> dynamicQueryLiveIn(Map<String,Object> map,int page,int rows){
		
		Page<LiveinrecordTable> pageE = PageHelper.startPage(page, rows, true);
		List<LiveinrecordTable> lists = liveinrecordTableMapper.dynamicFinfLiveIn(map);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("rows", lists);
		resultMap.put("total", pageE.getTotal());
		return resultMap;
	}
	/**
	 * 模糊分页查询住院病人 Byname
	 * @param likename
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> fuzzyQueryLiveInByname(String likename,int page,int rows){
		
		Page<LiveinrecordTable> pageE = PageHelper.startPage(page, rows, true);
		List<LiveinrecordTable> lists = liveinrecordTableMapper.fuzzyfindLiveInByname(likename);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("rows", lists);
		resultMap.put("total", pageE.getTotal());
		return resultMap;
	}
	/**
	 * 模糊分页查询住院病人 ByID
	 * @param likename
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> fuzzyQueryLiveInByLiveID(String likeID,int page,int rows){
		
		Page<LiveinrecordTable> pageE = PageHelper.startPage(page, rows, true);
		List<LiveinrecordTable> lists = liveinrecordTableMapper.fuzzyfindLiveInByLiveID(likeID);
		
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("rows", lists);
		resultMap.put("total", pageE.getTotal());
		return resultMap;
	}
	/**
	 * 查询出 住院号  并按规定拼接字符串
	 * @return
	 */
	public String createLiveID(){
		
		String newLiveID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iLiveinrecordDao.queryLiveID(LikeID);
		
		if(list.isEmpty()){
			newLiveID = "ZY" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 newLiveID = "ZY" + str + nextID;
		}
		return newLiveID;
	}
	/**
	 * 拼接探望号
	 * @return
	 */
	public Map<String,Object> createVisitID(){
		String VisitID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iVisitTableDao.queryVisitorID(LikeID);

		if(list.isEmpty()){
			VisitID = "TW" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 VisitID = "TW" + str + nextID;
		}
		Map<String,Object> map = new HashMap<>();
		map.put("VisitID", VisitID);
		return map;
	}
	/**
	 * 添加探望记录
	 * @param liveInID
	 * @param visitTable
	 */
	public void addvisitor(String liveInID,VisitTable visitTable){
		
		try {
			LiveinrecordTable liveinrecordTable = iLiveinrecordDao.findOne(liveInID);
			visitTable.setLiveinrecordTable(liveinrecordTable);
			iVisitTableDao.save(visitTable);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 分页查询探望
	 * @param date1
	 * @param data2
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String,Object> queryviasitor(String date1,String data2,int page,int rows){
		
		Page<VisitTable> p = PageHelper.startPage(page, rows);
		List<VisitTable> list = visitTableMapper.fuzzyfindVisitor(date1, data2);
		
		Map<String,Object> map = new HashMap<>();
		map.put("rows", list);
		map.put("total", p.getTotal());
		return map;
	}
	/**
	 * 根据住院号查询探望人
	 * @param liveID
	 * @return
	 */
	public List<VisitTable> queryvisitorByLiveID(String liveID){
		
		List<VisitTable> list = visitTableMapper.findVisitorByLiveID(liveID);
		return list;
		
	}
	/**
	 * 根据住院号查询住院信息
	 * @param liveID
	 * @return
	 */
	public LiveinrecordTable queryLiveByLiveID(String liveID){
		return liveinrecordTableMapper.findLiveByLiveID(liveID);
	}
	/**
	 * 动态分页查询主治医生
	 * @param page
	 * @param rows
	 * @param officeName
	 * @param dutyName
	 * @return
	 */
	public Map<String,Object> fuzzyQueryDoctor(int page,int rows,
			String officeName,String dutyName,String DoctorName){
		Map<String,Object> map = new HashMap<>();
		map.put("staffTypeName", "医生");
		map.put("officeName", officeName);
		map.put("staffDutyName", dutyName);
		map.put("DoctorName", DoctorName);
		
		Page<Map<String,Object>> p = PageHelper.startPage(page, rows, true);
		List<Map<String, Object>> list = liveinrecordTableMapper.fuzzyfindDoctor(map);
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("total", p.getTotal());
		resultMap.put("rows", list);
		
		return resultMap;
		
	}
	/**
	 * 根据住院ID查询床位信息
	 * @param liveID
	 * @return
	 */
	public Map<String,Object> queryBedIdByLiveID(String liveID){
		return liveinrecordTableMapper.findBedIDByLiveID(liveID);
		
	}

}
